using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Minion : Ennemy {

  

    int m_combo;

    int m_index;

    [SerializeField]
    float m_delayAtack = 1;

    [SerializeField]
    int m_maxIndexAttack;

	
	void followDave()
    {
        m_anim.SetBool("Running", true);
        transform.LookAt(m_dave.transform);
        transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z);
        transform.position= Vector3.Lerp(transform.position, m_dave.transform.position, m_speed * Time.deltaTime);
    }

    float distanceToDave()
    {
        return Vector3.Distance(transform.position, m_dave.transform.position);
    }

    protected override void attack()
    {
        base.attack();

        m_anim.SetBool("Running", false);

        m_anim.SetTrigger("Attack");

        if (m_index < m_maxIndexAttack)
            m_index++;
        else
            m_index = 0;

        m_anim.SetInteger("index", m_index);
        m_anim.SetInteger("Combo", 1);

        m_canAttack = false;

        StartCoroutine(WaitAndAttackAgain());
        

    }

    IEnumerator WaitAndAttackAgain()
    {
        yield return new WaitForSeconds(m_delayAtack);
        m_canAttack = true;              
    }

	// Update is called once per frame
	void Update ()
    {
        if (!m_active)
        {
            if (m_anim.enabled)
            {
                m_anim.SetBool("Idle", true);
                m_anim.SetBool("Running", false);
            }
            return;
        }

        m_anim.SetBool("Idle", false);


        if (distanceToDave() > 3)
            followDave();
        else if(m_canAttack)
            attack();

    }
}
